<template>
  <div>
    <a-button type="primary" v-if="!record" @click="dialog = true">新增角色 </a-button>
    <span v-else @click="dialog = true">编辑</span>
    <a-modal v-model="dialog" title="新增角色" @ok="submit" :width="800">
      角色名称：<a-input style="width: 20%" v-model="roleName" /> <br /><br />
      角色描述：<a-input style="width: 60%" v-model="description" /> <br /><br />
      数据权限：<a-select style="width: 20%" v-model="dataPermission">
        <a-select-option :value="0"> 访问所有数据 </a-select-option>
        <a-select-option :value="1"> 访问本账户数据 </a-select-option> </a-select
      ><br /><br />
      <div>
        <a-card title="菜单权限">
          <a-tree
            class="menus"
            v-model="menuPermissions"
            checkable
            :expanded-keys="expandedKeys"
            :auto-expand-parent="autoExpandParent"
            :tree-data="treeData"
            @expand="onExpand"
            @check="onCheck"
          />
        </a-card>
      </div>
    </a-modal>
  </div>
</template>
<script>
import { Tree } from 'ant-design-vue'
import { creatRole } from '@/api/common'
import treeData from './roleTree'
const lv3Map = {}
const lv2Map = {}
treeData.forEach(el => {
  el.children.forEach(lv2 => {
    lv2Map[lv2.key] = `${el.title}-${lv2.title}`
    lv2.children &&
      lv2.children.forEach(lv3 => {
        lv3Map[lv3.key] = lv3.title
      })
  })
})
export default {
  name: 'addRole',
  props: ['record'],
  components: {
    'a-tree': Tree
  },
  data() {
    return {
      dialog: false,
      roleName: '',
      description: '',
      menuPermissions: [],
      dataPermission: 0,
      expandedKeys: [],
      autoExpandParent: true,
      treeData
    }
  },
  methods: {
    async submit() {
      // const menuPermissions = this.parsePer(this.menuPermissions)
      // console.log(menuPermissions)
      if (this.roleName && this.description) {
        const form = {}
        form.roleName = this.roleName
        form.description = this.description
        form.dataPermission = this.dataPermission
        form.menuPermissions = this.parsePer(this.menuPermissions)
        // console.log(form.menuPermissions)
        const res = await creatRole(form)
        this.roleName = ''
        this.description = ''
        this.menuPermissions = []
        this.$emit('update')
        this.dialog = false
      } else {
        this.$message.warning('请输入角色名称、角色描述')
      }
    },
    parsePer(pers) {
      let perList = []
      for (let per of pers) {
        let [id, action] = per.split('.')
        if (!action) continue
        perList.push({
          permissionId: id,
          permissionName: lv2Map[id],
          action: action,
          describe: lv3Map[per],
          defaultCheck: 1
        })
      }

      return perList
    },
    onExpand(expandedKeys) {
      // console.log('onExpand', expandedKeys)
      this.expandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    onCheck(checkedKeys) {
      // console.log('onCheck', checkedKeys)
      this.checkedKeys = checkedKeys
    }
  }
}
</script>
<style lang="css" scoped>
.menus {
  height: calc(100vh - 500px);
  overflow: auto;
}
</style>
